<?php
/*
<Controller Information> 
Author: Naseer Ahmad
Date: 13-10-2011
Last Updated on:
*/

class categoryManagerController extends appController
{
	function index()
	{
		
		
		if(isset($this->data['cmd'])) $this -> set("cmd", $this->data['cmd']);
		
		if(isset($this->data['pCatId'])) $pCatId=$this->data['pCatId']; else $pCatId=0;
		$this -> set("pCatId", $pCatId);
		
		$parentName="Root";
		$levelUpId=-1;
		if($pCatId!=0){
			$parentName=$this -> categoryManager -> _getSingleData("CategoriesInfo", "categoryName", "categoryID=".$pCatId);
			$levelUpId= $this -> categoryManager -> _getSingleData("CategoriesInfo", "parentcategoryID", "categoryID=".$pCatId);
		}
		
		$this -> set("parentName", $parentName);
		$this -> set("levelUpId", $levelUpId);
		
		
		
		
		
		$arrCategories = $this ->  categoryManager -> _getArrayData("CategoriesInfo", "*", "parentcategoryID = ".$pCatId);
		$this -> set("arrCategories", $arrCategories);
		
		///////// edit category
		if(isset($this->data['catId'])){
			$editCat = $this -> categoryManager -> _getRowData("CategoriesInfo", "*", "categoryID=".$this->data['catId']);
			$this->set("editCat", $editCat);
			$this->set("action", "updateCategory");
		}
		else{
			$this->set("action", "saveCategory");
		}
		
		
		$this -> render('Category Manager');
	}
	
	function saveCategory(){
		//print "<pre>";
		//print_r($this->data);
		//print "</pre>";
		
		v("Category Name", $this->data['catName']);
		
		if(!isset($_SESSION[v])){
			$rs = $this -> categoryManager -> saveCategory($this->data['catName'], $this->data['catDetail'], $this->data['pCatId']);
			if($rs) $cmd="Category Saved Successfully!"; else $cmd=$this -> categoryManager -> getError();
		}
		else{
			$cmd = $_SESSION[v];
		}

		$rdArr = array("page"	=> "categoryManager",
				 "pCatId"	=> $this->data['pCatId'],
				 "cmd"		=> $cmd);
		$this->redirect($rdArr);
	}

	function updateCategory(){
		//print "<pre>";
		//print_r($this->data);
		//print "</pre>";
		
		v("Category Name", $this->data['catName']);
		
		if(!isset($_SESSION[v])){
			$rs = $this -> categoryManager -> updateCategory($this->data['catId'], $this->data['catName'], $this->data['catDetail']);
			if($rs) $cmd="Category Updated Successfully!"; else $cmd=$this -> categoryManager -> getError();
		}
		else{
			$cmd = $_SESSION[v];
		}

		$rdArr = array("page"	=> "categoryManager",
				 "pCatId"	=> $this->data['pCatId'],
				 "cmd"		=> $cmd);
		$this->redirect($rdArr);
	}


	function delCategory(){
		//print "<pre>";
		//print_r($this->data);
		//print "</pre>";
		
		$where =array("categoryID"	=> $this -> data['catId']);
		$rs= $this -> categoryManager -> _XTable("CategoriesInfo", $where);
		if($rs) $cmd="Category Deleted Successfully!"; else $cmd=$this -> categoryManager -> getError();
		$rdArr = array("page"	=> "categoryManager",
				 "pCatId"	=> $this->data['pCatId'],
				 "cmd"		=> $cmd);
		$this->redirect($rdArr);
	}
	
	function adCount($catId){
		
	}

	
}
?>